package cate.game.play.proce.round;


import cate.common.table.d.GDBuff;
import cate.game.play.part.TeamVS;
import cate.game.play.proce.action.ctx.TimeActionCtx;


public class RoundBuffHandler {

    private RoundCtx round;

    public RoundBuffHandler(RoundCtx round) {
        this.round = round;
    }



    /** 回合开始 */
    public void onBegin() {
        useAndClean(this.round.getPlay().teamVS, GDBuff.Phase.ROUND_BEG);
    }

    /** 回合结束 */
    public void onEnd() {
        useAndClean(this.round.getPlay().teamVS, GDBuff.Phase.ROUND_END);
    }

    private void useAndClean(TeamVS teamVS, byte phase) {
        use(teamVS,phase);
        clean(teamVS,phase);
    }

    private void use(TeamVS teamVS, byte phase) {
        TimeActionCtx action = round.buildTimeAction();
        teamVS.forAreaFighters(hero -> {
            hero.buff.forHandlers(
                    handler -> {
                        switch (phase) {
                            case GDBuff.Phase.ROUND_BEG:
                                handler.onRoundBegin(action);
                                break;
                            case GDBuff.Phase.ROUND_END:
                                handler.onRoundEnd(action);
                                break;
                            default:
                                break;
                        }
                    });
        });
    }

    private void clean(TeamVS teamVS, byte phase){
        // buff时间流逝处理
        TimeActionCtx timeAction = this.round.buildTimeAction();
        teamVS.forAreaFighters(f-> f.buff.updateCurRoundIndex(timeAction, phase));
        // 删除失效的buff
        teamVS.forAreaFighters(f-> f.buff.removeInvalid());
    }


}
